Session 6: Application 2
Department of Econometrics and Business Statistics
Indonesia earthquakes datasets
These datasets contain spatio-temporal information about earthquakes in Indonesia.
quakes_df.rds contains records for individual earthquakes. provinces_df.rds contains records for each province, including aggregations of earthquake counts and densities.
Note: sf objects don’t work with plotly if there are multiple geometry types in them.
quakes_11_2023_shared <- highlight_key(quakes_11_2023)
p1 <- ggplot(quakes_11_2023_shared, aes(x=ymd, y=mag)) +
geom_point() +
theme_minimal()
gp1 <- ggplotly(p1, width=400, height=400) |>
config(displayModeBar = FALSE) |>
highlight(on = "plotly_selected",
off = "plotly_doubleclick")
# multiple types of geometry in provinces_sf needs fixing
provinces_sf2 <- st_cast(provinces_sf, "MULTIPOLYGON")
p2 <- ggplot() +
geom_sf(data=provinces_sf2) +
geom_point(data=quakes_11_2023_shared, aes(x=X, y=Y)) +
theme_map()
gp2 <- ggplotly(p2, width=800, height=400) |>
config(displayModeBar = FALSE) |>
highlight(on = "plotly_selected",
off = "plotly_doubleclick")
bscols(gp1, gp2, widths = c(4, 8))quakes_2023 <- quakes_df |>
filter(year(ymd) == 2023) |>
group_by(province) |>
summarise(mag = mean(mag, na.rm=TRUE),
n = n(), na.rm=TRUE)
provinces_sf2 <- left_join(provinces_sf2, quakes_2023)
p3 <- ggplot(provinces_sf2) +
geom_sf(aes(fill=mag, label=province)) +
theme_map() +
scale_fill_viridis_c()Population data can be found here.
pop <- read_csv("data/province_pop.csv") |>
rename(province = Provinsi)
provinces_sf2 <- left_join(provinces_sf2, pop)
indonesia_carto <- provinces_sf2 |>
st_transform(3395) |>
cartogram_cont("2023") |>
st_transform("WGS84")
indonesia_carto <- st_cast(indonesia_carto, "MULTIPOLYGON")
ggplot(indonesia_carto) + geom_sf()
indonesia_carto <- left_join(indonesia_carto, quakes_2023)
p4 <- ggplot(indonesia_carto) +
geom_sf(aes(fill=mag, label=province)) +
theme_map() +
scale_fill_viridis_c()Once you have your quarto document rendering, change the data to use 2022, and re-render, to see how fast it is to re-do the analysis if the data changes.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.